export default {
     getTimeUnix(time, cut){
        var time = new Date(time.replace(/-/g, '/') || Date.now())
        return cut? Math.round( time /1000) : time
     },
      
     fromTime(time = '', fmt = 'yyyy-MM-dd') { //  HH:mm:ss
        var date = new Date(time.replace(/-/g, '/') || Date.now()); // 兼容 ios 端字符串获取时间的问题
        const o = {
          'M+': date.getMonth() + 1, // 月份
          'd+': date.getDate(), // 日
          'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12, // 小时
          'H+': date.getHours(), // 小时
          'm+': date.getMinutes(), // 分
          's+': date.getSeconds(), // 秒
          'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
          'S': date.getMilliseconds(), // 毫秒
        };
        if (/(y+)/.test(fmt)) {
          fmt = fmt.replace(RegExp.$1, (`${date.getFullYear()}`).substr(4 - RegExp.$1.length));
        }
        Object.keys(o).forEach((k) => {
          if (new RegExp(`(${k})`).test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : ((`00${o[k]}`).substr((`${o[k]}`).length)));
          }
        }); 
        return fmt;
     },
    fromNow(time, fmt) { // 语义化显示时间差
        const d = new Date(time);
        const diff = (Date.now() - d) / 1000;
        if (diff < 0) return time;
        if (diff < 30) return '刚刚';
        if (diff < 3600) return `${Math.ceil(diff / 60)}分钟前`;
        if (diff < 3600 * 24)  return `${Math.floor(diff / 3600)}小时前`;
        if (diff < 3600 * 24 * 2)  return '1天前';
        return fmt?parseTime(time, fmt): `${d.getFullYear()}年${d.getMonth() + 1}月${d.getDate()}日${d.getHours()}时${d.getMinutes()}分${d.getSeconds()}秒`;
    },
    getQueryObject(url) { // 格式化 urlQuery
        url = url == null ? window.location.href : url;
        const search = url.substring(url.lastIndexOf('?') + 1);
        const obj = {};
        const reg = /([^?&=]+)=([^?&=]*)/g;
        search.replace(reg, (rs, $1, $2) => {
          const name = decodeURIComponent($1);
          let val = decodeURIComponent($2);
          val = String(val);
          obj[name] = val;
          return rs;
        });
        return obj;
    },
    serialize(query = {}) { // 序列化 对象
        const temp = [];
        for (const key in query) {
          if (Object.hasOwnProperty.call(query, key)) {
            const enValue = encodeURIComponent(query[key]);
            temp.push(`${key}=${enValue}`);
          }
        }
        return temp.join('&');
     },
    deserialize(query =''){// 反序列化 字符串
        var obj = {},reg = /([^=&\s]+)[=\s]*([^&\s]*)/g;
        if (!query || JSON.stringify(query) === '{}') return obj;
        while (reg.exec(query)) {
            obj[RegExp.$1] = decodeURIComponent(RegExp.$2);
        }
        return obj
    },
    formatPhone(phoneStr,separator = ' '){
        return `${phoneStr.substr(0, 3)}${separator}${phoneStr.substr(3, 4)}${separator}${phoneStr.substr(7, 4)}`;
    },
    formatHidePhone(phoneStr) {
        return `${phoneStr.substr(0, 3)}****${phoneStr.substr(7, 11)}`;
    },
    formatBankCard(cardNo) {
        return cardNo.toString().replace(/\s/g, '').replace(/(.{4})/g, '$1 ');
    },
}